home *** CD-ROM | disk | FTP | other *** search
- /************************************************************/
- /* */
- /* ColorInfo Code from Chapter Four of */
- /* */
- /* *** The Macintosh Programming Primer *** */
- /* */
- /* Copyright 1990, Dave Mark */
- /* */
- /* This program demonstrates specific Mac programming */
- /* techniques. */
- /* */
- /************************************************************/
-
- #include "Picker.h"
-
- #define BASE_RES_ID 400
- #define NIL_POINTER 0L
- #define NIL_STRING "\p"
- #define INVISIBLE FALSE
- #define NO_GOAWAY FALSE
- #define MOVE_TO_FRONT (WindowPtr)-1L
- #define REMOVE_ALL_EVENTS 0
- #define INDEX_DEVICE TRUE
- #define DIRECT_DEVICE FALSE
-
-
- Boolean IsColor();
-
-
- main()
- {
- int pixDepth;
- GDHandle curDev;
- Rect bounds;
-
- ToolBoxInit();
-
- if ( IsColor() )
- {
- curDev = GetDeviceList();
-
- while( curDev != NIL_POINTER )
- {
- bounds = (**curDev).gdRect;
-
- pixDepth = GetPixelDepth( curDev );
- switch( pixDepth )
- {
- case 1:
- DisplayColors( &bounds, 1, 2, 128, INDEX_DEVICE );
- break;
- case 2:
- DisplayColors( &bounds, 2, 2, 128, INDEX_DEVICE );
- break;
- case 4:
- DisplayColors( &bounds, 4, 4, 64, INDEX_DEVICE );
- break;
- case 8:
- DisplayColors( &bounds, 16, 16, 24, INDEX_DEVICE );
- break;
- default:
- DisplayColors( &bounds, 48, 48, 8, DIRECT_DEVICE );
- break;
- }
- curDev = GetNextDevice( curDev );
- }
- while( ! Button() ) ;
- }
- else
- DoAlert( "\pThis machine does not support Color QuickDraw!" );
- }
-
-
- /*********************************** ToolBoxInit */
-
- ToolBoxInit()
- {
- InitGraf( &thePort );
- InitFonts();
- FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NIL_POINTER );
- InitCursor();
- }
-
-
- /******************************** GetPixelDepth *********/
-
- int GetPixelDepth( theDevice )
- GDHandle theDevice;
- {
- PixMapHandle screenPMapH;
- int pixelDepth;
-
- screenPMapH = (**theDevice).gdPMap;
- pixelDepth = (**screenPMapH).pixelSize;
- return( pixelDepth );
- }
-
-
- /******************************** IsColor *********/
-
- Boolean IsColor()
- {
- SysEnvRec mySE;
-
- SysEnvirons( 1, &mySE );
- return( mySE.hasColorQD );
- }
-
-
- /*********************************** DisplayColors */
-
- DisplayColors( boundsPtr, width, height, pixPerBox, isIndex )
- Rect *boundsPtr;
- int width, height, pixPerBox;
- Boolean isIndex;
- {
- Rect r;
- int row, col;
- WindowPtr cWindow;
- RGBColor curColor;
- HSVColor hsvColor;
- long colorNum;
-
- hsvColor.value = hsvColor.saturation = 65535;
-
- r.top = 0;
- r.left = 0;
- r.right = width * pixPerBox;
- r.bottom = height * pixPerBox;
-
- cWindow = NewCWindow( NIL_POINTER, &r, "\pDevice Colors",
- INVISIBLE, noGrowDocProc, MOVE_TO_FRONT,
- NO_GOAWAY, NIL_POINTER );
-
- CenterWindow( cWindow, boundsPtr );
- ShowWindow( cWindow );
- SetPort( cWindow );
-
- for ( row=0; row<height; row++ )
- {
- for ( col=0; col<width; col++ )
- {
- r.top = row * pixPerBox;
- r.left = col * pixPerBox;
- r.bottom = r.top + pixPerBox;
- r.right = r.left + pixPerBox;
-
- if ( isIndex )
- Index2Color( (long)(row*width + col), &curColor );
- else
- {
- colorNum = (long)(row*width + col);
- hsvColor.hue = 65535 * colorNum / (width * height );
- HSV2RGB( &hsvColor, &curColor );
- }
- RGBForeColor( &curColor );
- PaintRect( &r );
- }
- }
- }
-
-
- /*********************************** CenterWindow */
-
- CenterWindow( w, boundsPtr )
- Rect *boundsPtr;
- WindowPtr w;
- {
- Rect r;
- int width, height, sWidth, sHeight, h, v;
-
- r = w->portRect;
-
- width = r.right - r.left;
- height = r.bottom - r.top;
-
- sWidth = boundsPtr->right - boundsPtr->left;
- sHeight = boundsPtr->bottom - boundsPtr->top;
-
- h = boundsPtr->left + ((sWidth - width) / 2);
- v = boundsPtr->top + ((sHeight - height) / 2);
-
- MoveWindow( w, h, v, FALSE );
- }
-
-
- /*********************************** DoAlert */
-
- DoAlert( s )
- Str255 s;
- {
- ParamText( s, NIL_STRING, NIL_STRING, NIL_STRING );
- NoteAlert( BASE_RES_ID, NIL_POINTER );
- }